<?php
class ScholarshipController extends Controller{
  public $isActivated = 0;
          public function accessRules()
		 {
			return array(
			   array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights>=2',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
				
			);
		 }
    public function actionIndex(){
		 $this->breadcrumbs=array(
               '审核综合奖学金'=>array('index'),
               '所有学生',
             );
		$classRooms = Classroom::model()->findAll();
		$major = Major::model()->findAll();
		$ci  = new CDbCriteria();
		if(!empty($_GET) && (count($_GET) >= 4)){
		  foreach($_GET as $key => $value){
			  if($value == '' || $key == 'page')
				  continue;
			  if($key == 'stuNumber'){
				  $ci->params[':s'] = $_GET['stuNumber'];
				  $ci->addCondition('username = :s');		  
			  }else{
			   $p = ':'.substr($key,0,1);	  
			   $ci->params[$p] = $value;
               $ci->addCondition('stuinfo.'.$key.' = '.$p);  
			  }
			  
		  }
		  $ci->with = array('stuinfo');
		 }
		 
		 $ci->select = array('`t`.uId','username');
		 $ci->join = 'RIGHT JOIN `stuscholarship` on `t`.uId = stuscholarship.`uId`';
		 $ci->group = 'stuscholarship.`uId`,`condition`';
		 $ci->addCondition('rights =  1');
		 $command = Yii::app()->db->createCommand('SELECT `t`.uId, username,count(stuscholarship.uId) as num FROM `user` `t` RIGHT JOIN `stuscholarship` on `t`.uId = stuscholarship.`uId` WHERE rights = 1 GROUP BY stuscholarship.`uId`,`condition` HAVING stuscholarship.`condition` = 1');
		 $num = $this->setNum($command->queryAll());
		 $total = User::model()->count($ci);
		 $pages =  new CPagination($total);
		 $pages->pageSize = 20;
		 $pages->applyLimit($ci);
		 $stus = User::model()->findAll($ci);
		
		$this->render("index",array('num'=>$num,'major'=>$major,'classRooms'=>$classRooms,'total'=>$total,'stus'=>$stus,'pages'=>$pages));
		
	}
	  private function setNum($data){
		  $i = 0;
		  $newData = array();
		  foreach($data as $d)
			  $newData[$d['uId']] = $d['num'];
			  
		  return $newData;
	  }
      public function actionStudetail(){
		$request = Yii::app()->request;
		if(is_numeric($request->getParam('uId'))){
			$uId = $request->getParam('uId');
			$user = User::model()->findByPk($uId);
			if(!$user){
				$this->redirect('scholarship/index');
			}else{
			   $scs = array();
           	   $terms = array('第一学期','第二学期','第三学期','第四学期','第五学期','第六学期','第七学期','第八学期');
			   foreach($terms as $t){
				 //取出未审核的
				 $scs[] = Stuscholarship::model()->findAll('uId = :u and term = :t and t.condition = :c',array(':u'=>$uId,':t'=>$t,':c'=>'1'));  
		       
			   }
			  
			}
			
		}else{
			
			$this->redirect('index');
		}  
		
		$this->render("studetail",array('user'=>$user,'scs'=>$scs));
		
	}	
	public function actionAddcomment(){
		 if(Yii::app()->request->isAjaxRequest){
			   $stuSch  = Stuscholarship::model()->findByPk($_POST['stuScholarItemId']);
			   $comment = $_POST['comment'];
			   if($stuSch){
			   $stuN = new Stunotification;
			   $stuN->uId = $stuSch->uId;
			   $stuN->detailId = $stuSch->stuScholarItemId;
			   $stuN->module = 2; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
			   $stuN->comment = $comment;
			   $stuN->addtime = date('Y-m-d H:i:s');
			   $stuN->isRead = 0;
			   $stuSch->condition = 2 ;// 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
			   if($stuN->save()&&$stuSch->save()){
				  
				  echo  json_encode(array('code'=>200,'msg'=>'添加成功'));
			   }else{
					$this->logAr($stuN);
					$this->logAr($stuSch);
					echo  json_encode(array('code'=>502,'msg'=>'添加失败'));
				}
			   
			   
			   }else{
				   echo  json_encode(array('code'=>404,'msg'=>'没有找到这个条目'));
				   
			   }
			   
		   }else{
			   $this->redirect(array('scholarship/index'));
			   
		   }
		
	}
	  
	  public function actionPassone(){
		 if(Yii::app()->request->isAjaxRequest){
			   $stuSchItem = Stuscholarship::model()->findByPk(Yii::app()->request->getParam('stuScholarItemId'));
			   if($stuSchItem){
				   $stuSchItem->condition = 3 ; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				   if($stuSchItem->save()){
						
						echo  json_encode(array('code'=>200,'msg'=>' 审核成功'));
						
				   }else{
					   $this->logAr($stuSchItem);
					   echo  json_encode(array('code'=>502,'msg'=>'审核失败'));
				   }
			   }else{
				   echo  json_encode(array('code'=>404,'msg'=>'审核失败，没有找到条目'));
				   
			   }
			   
		   }else{
			   $this->logError('非法请求，综合奖学金 审核 ajax');
			   $this->redirect(array('scholarship/index'));
			   
		   }
		   
		  
	  }
	  public function actionPassmuti(){
		   $successNum = 0;
		   $successArray = array();
		   if(Yii::app()->request->isAjaxRequest){
			   $stus = '&'.$_POST['stus'];
			   $stuArray = explode('&l=',$stus);
			  foreach($stuArray as $schItemId){
				  if($schItemId == '')
					  continue;
			   $stuSch = Stuscholarship::model()->findByPk($schItemId);
			   if($stuSch){
				   $stuSch->condition = 3; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				   if($stuSch->save()){
					    $successArray[$successNum] = $schItemId;
						$successNum++;
				    }else{
					   $this->logAr($stuSch);
					   echo  json_encode(array('code'=>502,'msg'=>' 审核失败'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
					   return;
				   }
			    }else{
				   echo  json_encode(array('code'=>404,'msg'=>'审核失败，没有找到条目'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
				   return;
			    }
			  }
		   echo  json_encode(array('code'=>200,'msg'=>'审核成功的学生数目：'.$successNum,'successArray'=>$successArray));
		   }else{
			   $this->logError('非法请求，gsp 多位 学生审核 ajax');
			   $this->redirect(array('scholarship/index'));
			   
		   }
		   
	   }
	   public function actionUnpassmuti(){
		   $successNum = 0;
		   $successArray = array();
		   if(Yii::app()->request->isAjaxRequest){
			   $stus = '&'.$_POST['stus'];
			   $stuArray = explode('&l=',$stus);
			  foreach($stuArray as $schItemId){
				  if($schItemId == '')
					  continue;
			   $stuSch =Stuscholarship::model()->findByPk($schItemId);
			   if($stuSch){
				   $stuN = new Stunotification;
				   $stuN->uId = $stuSch->uId;
				   $stuN->detailId = $stuSch->stuScholarItemId;
				   $stuN->module = 2; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
				   $stuN->comment = '同学，您的这一条综合奖学金申请没有通过审核，请删除后，再次提交';
				   $stuN->addtime = date('Y-m-d H:i:s');
				   $stuN->isRead = 0;
				   
				   $stuSch->condition = 2; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				   if($stuSch->save()&&$stuN->save()){
					    $successArray[$successNum] = $schItemId;
						$successNum++;
				    }else{
					   $this->logAr($stuSch);
					   echo  json_encode(array('code'=>502,'msg'=>' 审核失败'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
					   return;
				   }
			    }else{
				   echo  json_encode(array('code'=>404,'msg'=>'添加不通过审核失败，没有找到条目'.'<br>成功的学生数目：'.$successNum,'successArray'=>$successArray));
				   return;
			    }
			  }
		   echo  json_encode(array('code'=>200,'msg'=>'添加不通过审核成功的学生数目：'.$successNum,'successArray'=>$successArray));
		   }else{
			   $this->logError('非法请求，综合奖学金 多位 学生审核 ajax');
			   $this->redirect(array('scholarship/index'));
			   
		   }
		   
	   }
	 public function actionUpdatescholaritem(){
		 
		 if(Yii::app()->request->isAjaxRequest){
			   $stuSchItem  = Stuscholarship::model()->findByPk(Yii::app()->request->getParam('stuScholarItemId'));
			   $request = Yii::app()->request;
			   if($stuSchItem){
				   $stuSchItem->detail = $request->getParam('detail');
				   $stuSchItem->credit = $request->getParam('credit');
				   $stuN = new Stunotification;
				   $stuN->uId = $stuSchItem->uId;
				   $stuN->detailId = $stuSchItem->stuScholarItemId;
				   $stuN->module = 2; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
				   $stuN->comment = '同学，您的这一条综合奖学金经过老师修改之后，通过了审核';
				   $stuN->addtime = date('Y-m-d H:i:s');
				   $stuN->isRead = 0;
				   
				   $stuSchItem->condition = 3; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				   if($stuSchItem->save()&&$stuN->save()){
					   
					   echo  json_encode(array('code'=>200,'msg'=>'添加成功，并且已给学生添加通知'));
					   
				   }else{
					    $this->logAr($stuSchItem);
						$this->logAr($stuN);
					    echo  json_encode(array('code'=>501,'msg'=>'添加失败'));
				   }
				     
			   }else{
				   
				   echo  json_encode(array('code'=>404,'msg'=>'没有找到这个条目'));
			   }
			   
		   }else{
			   $this->redirect(array('scholarship/index'));
		   }
		 
	 }
	 
	 public function actionTeacheradditem(){
		 
		 if(Yii::app()->request->requestType == 'POST'){
			 $request = Yii::app()->request;
			 $uId = $request->getParam('uId');
			 $user = User::model()->findByPk($uId);
			 if($user){
                $stuSch = new Stuscholarship;
				$stuSch->uId = $user->uId;
				$stuSch->term = $request->getParam('term');
				$stuSch->module = $request->getParam('module');
				$stuSch->detail = $request->getParam('detail');
				$stuSch->credit = $request->getParam('credit');
				$stuSch->addtime = date('Y-m-d H:i:s');
				$stuSch->condition = 3; // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
				if($stuSch->save()){
					$stuN = new Stunotification;
					$stuN->uId = $user->uId;
					$stuN->detailId = $stuSch->stuScholarItemId;
					$stuN->module = 2; //1 -- 个人信息 2 -- 综合奖学金 3 -- GSP 4---综合素质 5 -- 学生报名
					$stuN->comment = '同学，老师给你添加了一条综合奖学金条目';
					$stuN->addtime = date('Y-m-d H:i:s');
					$stuN->isRead = 0;
					if($stuN->save()){
						Yii::app()->user->setFlash('teacherAdd','添加成功，而且已给学生添加通知');
						
					}else{
						$this->logAr($stuN);
						Yii::app()->user->setFlash('teacherAdd','添加成功，但是学生通知保存失败');
					}
				}else{
					$this->logAr($stuSch);
					Yii::app()->user->setFlash('teacherAdd','添加失败，可能是学分格式不正确，应该是实数格式，谢谢');
					
				}
				


			   
			 }else{
				$this->redirect(array('scholarship/index'));  
			}
			 
			 
		 }else{
			$this->redirect(array('scholarship/index')); 
			 
		 }
		
		 $this->redirect(Yii::app()->request->urlReferrer);
		 
	 }
	public function actionAddcate(){
		
		
		$list = Scholaritem::model()->findAll();
		
		$this->render("addcate",array('list'=>$list));
		
	}
	public function actionAddcatedeal(){
		if(Yii::app()->request->requestType == 'POST'){
			$sc = new Scholaritem;
			$sc->module = $_POST['module'];
			$sc->info = CHtml::encode($_POST['info']);
			$sc->addtime = date('Y-m-d H:i:s');
			if($sc->save()){
				Yii::app()->user->setFlash('update','综合奖学金条目添加成功,可以到已添加的综合奖学金条目中管理');
			}else{
			    Yii::app()->user->setFlash('update','综合奖学金条目添加失败');
			}
			
		}
		$this->redirect(array('scholarship/addcate'));
		
	}
	public function actionDeleteitem(){
		
		if(Yii::app()->request->isAjaxRequest){
			$sId = $_POST['sId'];
			$sc = Scholaritem::model()->findByPk($sId);
			if($sc){
				if($sc->delete()){
					echo json_encode(array('code'=>200,'msg'=>'删除成功'));
				}else{
					$this->logAr($sc);
					echo json_encode(array('code'=>502,'msg'=>'删除失败'));
				}
				
			}else{
				echo json_encode(array('code'=>404,'msg'=>'找不到条目'));
				
			}
			
		}else{
			
			echo json_encode(array('code'=>306,'msg'=>'非法的请求'));
		}
		
	}
	public function actionChangepro(){
		$per = Scholarshippercent::model()->find();
		if(!$per){
			$per = new Scholarshippercent;
		}
		if(Yii::app()->request->requestType == 'POST'){
			$this->dealChangePro($per,$_POST);
		}
		
		$this->render("changepro",array('per'=>$per));
		
	}
	protected function dealChangePro($per,$post){
		
		$per->morality  = $post['morality'];
		$per->integrate  = $post['integrate'];
		$per->physical  = $post['physical'];
		$per->performance  = $post['performance'];
		
			if($per->save()){
				Yii::app()->user->setFlash('update','修改成功');
			}else{
				$this->logAr($per);
				Yii::app()->user->setFlash('update','修改失败');
			}
			$this->redirect(array('scholarship/changepro'));
	}
	public function actionReport(){
		 $this->breadcrumbs=array(
               '审核综合奖学金'=>array('index'),
               '学生报表',
             );
		$classRooms = Classroom::model()->findAll();
		$ci  = new CDbCriteria();
		$major = Major::model()->findAll();
		if(!empty($_GET) && (count($_GET) >= 4)){
		  foreach($_GET as $key => $value){
			  if($value == '' || $key == 'page')
				  continue;
			  if($key == 'stuNumber'){
				  $ci->params[':s'] = $_GET['stuNumber'];
				  $ci->addCondition('username = :s');		  
			  }else{
			   $p = ':'.substr($key,0,1);	  
			   $ci->params[$p] = $value;
               $ci->addCondition('stuinfo.'.$key.' = '.$p);  
			  }
			  
		  }
		  $ci->with = array('stuinfo');
		 }
		 
		 $ci->addCondition('rights =  1');
		 $total = User::model()->count($ci);
		 $pages =  new CPagination($total);
		 $pages->pageSize = 20;
		 $pages->applyLimit($ci);
		 $stus = User::model()->findAll($ci);
		$this->render("report",array('major'=>$major,'classRooms'=>$classRooms,'total'=>$total,'stus'=>$stus,'pages'=>$pages));
		
	} 
	public function actionOnestureport(){
		$request = Yii::app()->request;
		if(is_numeric($request->getParam('uId'))){
			$uId = $request->getParam('uId');
			$user = User::model()->findByPk($uId);
			if(!$user){
				$this->redirect('scholarship/index');
			}else{
			   $scs = array(); //奖学金列表
			   $statistic = array(); //统计结果
			    
               $terms = array('第一学期'=>'term_1','第二学期'=>'term_2','第三学期'=>'term_3','第四学期'=>'term_4','第五学期'=>'term_5','第六学期'=>'term_6','第七学期'=>'term_7','第八学期'=>'term_8');
			   $stuPer = Stuperformance::model()->find('uId = :u',array(':u'=>$uId));
			   if(!$stuPer)
				   $stuPer = new Stuperformance;
			  $schPercent = Scholarshippercent::model()->find(); //学生成绩
			  
			   foreach($terms as $t => $value){
				 $st = array('mor'=>0.0,'inte'=>0.0,'phy'=>0.0);				 
				 $scsTemp = Stuscholarship::model()->findAll('uId = :u and term = :t',array(':u'=>$uId,':t'=>$t));
				 
				 $scs[] = $scsTemp;	
				 
					foreach($scsTemp as $s){
						if($s->condition !== '3')
							continue;
						switch($s->module){
							 case '品德行为':$st['mor'] += $this->getEndPer($s,$schPercent->morality);break;
							 case '文体综合素质':$st['phy'] += $this->getEndPer($s,$schPercent->physical);break;
							 case '学业表现':$st['inte'] += $this->getEndPer($s,$schPercent->integrate);break;
							 
						}		 
					   
				   }
				   if($stuPer->$value !== '-1')
						$st['inte'] += (float)$schPercent->performance*(float)$stuPer->$value/100.0;
				   
				   $statistic[] = $st;
				   
			   }
			   
			  
			}
			
		}else{
			
			$this->redirect('report');
		}  
		
		
		
		$this->render("onestureport",array('user'=>$user,'scs'=>$scs,'sta'=>$statistic,'stuPer'=>$stuPer));
		
	} 
	private function getEndPer($scs,$per){
		
		return (float)$scs->credit*$per/100.0;
		
	}
	private function  getTermName($term){
		$r = '第一学期';
		switch($term){
			case 'term_1':$r = '第一学期';break;
			case 'term_2':$r = '第二学期';break;
			case 'term_3':$r = '第三学期';break;
			case 'term_4':$r = '第四学期';break;
			case 'term_5':$r = '第五学期';break;
			case 'term_6':$r = '第六学期';break;
			case 'term_7':$r = '第七学期';break;
			case 'term_8':$r = '第八学期';break;
			
		}
		return $r;
	}
	public function actionExportexcel(){
		
		$ci  = new CDbCriteria();
		$term = 'term_1';
		if(!empty($_GET) && (count($_GET) == 5)){
		  foreach($_GET as $key => $value){
			  if($value == '')
				  continue;
			  if($key == 'stuNumber'){
				  $ci->params[':s'] = $_GET['stuNumber'];
				  $ci->addCondition('username = :s');		  
			  }else{
				if($key == 'term')
					continue;
			   $p = ':'.substr($key,0,1);	  
			   $ci->params[$p] = $value;
               $ci->addCondition('stuinfo.'.$key.' = '.$p);  
			  }
			  
		  }
		  $term = $_GET['term'];
		  $ci->with = array('stuinfo');
		 }
		 $termName = $this->getTermName($term);
		 $ci->addCondition('rights =  1');
		 $stus = User::model()->findAll($ci);
		 $percent = Scholarshippercent::model()->find();
		  Yii::$enableIncludePath = false;
		  Yii::import('application.extensions.phpExcel.PHPExcel',1);
		  $objPHPExcel = new PHPExcel();
		  $objPHPExcel->getProperties()->setCreator("kezhi Company")
							 ->setLastModifiedBy("第二师范学院学生信息管理系统")
							 ->setTitle(date('Y-m-d H:i:s'))
							 ->setSubject("学生信息导出")
							 ->setDescription("学生信息导出")
							 ->setKeywords("学生信息导出")
							 ->setCategory("export 综合奖学金 file");
		 
		  $objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A1', '学号')
					->setCellValue('B1', '姓名')
					->setCellValue('C1', '年级')
					->setCellValue('D1', '班级')
					->setCellValue('E1', '专业')
					->setCellValue('F1', '品德')
					->setCellValue('G1', '学业')
					->setCellValue('H1', '文体')
					->setCellValue('I1', '课业成绩')
					->setCellValue('J1', '合计');
		$i = 2;			
		foreach($stus as $u){
			 $mor = 0;
			 $inte = 0;
			 $phy = 0;
			 $per = 0;
			 $stuschs = Stuscholarship::model()->findAll('uId = :u and term = :t',array(':u'=>$u->uId,':t'=>$termName));
			 foreach($stuschs as $s){
				 if($s->condition == '3'){
				    switch($s->module){
						
						case '品德行为':$mor+=$s->credit;break;
						case '学业表现':$inte+=$s->credit;break;
						case '文体综合素质':$phy+=$s->credit;break;
						
						
					}
				 }
			 }
			 
			 $performance = Stuperformance::model()->find('uId = :u',array(':u'=>$u->uId));
			 if(!$performance)
				 $performance = new Stuperformance;
			 $per = $performance->$term;
			 
			 $inte += $per;
			 $mor = $mor*$percent->morality/100;
			 $inte = $inte*$percent->integrate/100;
			 $phy = $phy*$percent->integrate/100;
			
			 if($u->stuinfo){
				     $realName = $u->stuinfo->realName;
					 $grade = $u->stuinfo->grade;
					 $classRoom = $u->stuinfo->classRoom;
					 $major = $u->stuinfo->major; 
				    
			 }else{
				     $realName = '未填写';
					 $grade = '未填写';
					 $classRoom = '未填写';
					 $major = '未填写'; 
				 
			 }
			$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A'.$i, $u->username)
					->setCellValue('B'.$i, $realName)
					->setCellValue('C'.$i, $grade)
					->setCellValue('D'.$i, $classRoom)
					->setCellValue('E'.$i, $major)
					->setCellValue('F'.$i, $mor)
					->setCellValue('G'.$i, $inte)
					->setCellValue('H'.$i, $phy)
					->setCellValue('I'.$i, $per)
					->setCellValue('J'.$i, $mor+$inte+$phy);	
					$i++;		
			
		 }			
         		

		$this->downloadExcel(date('Y-m-d').'学生综合奖学金导出 '.$termName,$objPHPExcel);
		
		
	}
	public function actionExportword(){
		
	
		//var_dump($_GET);die();
		if(!empty($_GET) && (count($_GET) == 3)){
				$major = $_GET['major'];
				$classRoom = $_GET['classRoom'];
				$grade = $_GET['grade'];
				$dataTemplete = array(
										'uId'=>'',
										'username'=>'',
										'realName'=>'',
										);
				$datas = array();
				$t_1 = array();
				$t_2 = array();  $tt_1 = array();
 				$t_3 = array();
				$t_4 = array();  $tt_2 = array();
				$t_5 = array();
				$t_6 = array();  $tt_3 = array();
				$t_7 = array();
				$t_8 = array();  $tt_4 = array();
				$ci =new CDbCriteria;
				$ci->addCondition("`stuinfo`.major = '".$major."'");
				$ci->addCondition("`stuinfo`.classRoom = '".$classRoom."'");
				$ci->addCondition("`stuinfo`.grade = '".$grade."'");
				$ci->addCondition("rights = 1");
				$ci->with = array('stuinfo');
				// 这个专业的这个班级
				$users = User::model()->findAll($ci);
				$percent = Scholarshippercent::model()->find();
				foreach($users as $u){
					
					
					$dataTemplete['username'] = $u->username;
					$dataTemplete['uId'] = $u->uId;
					if($u->stuinfo)
						$dataTemplete['realName'] = $u->stuinfo->realName;
					else
						$dataTemplete['realName'] = '未添加';
					$datas[$u->uId] = $dataTemplete;
					
					$t_1[$u->uId] = 0;
					$t_2[$u->uId] = 0;  $tt_1[$u->uId] = 0;
					$t_3[$u->uId] = 0;
					$t_4[$u->uId] = 0;  $tt_2[$u->uId] = 0;
					$t_5[$u->uId] = 0;
					$t_6[$u->uId] = 0;  $tt_3[$u->uId] = 0;
					$t_7[$u->uId] = 0;
					$t_8[$u->uId] = 0;  $tt_4[$u->uId] = 0;
					
					if(!$u->stuper)
						$u->stuper = new Stuperformance;
					
					$sches = $u->stusch;
					foreach($sches as $s){
						if($s->condition == 3){
							switch($s->term){
							
								case '第一学期':$t_1[$u->uId] += $this->getCredit($s,$percent);break;
								case '第二学期':$t_2[$u->uId] += $this->getCredit($s,$percent);break;
								case '第三学期':$t_3[$u->uId] += $this->getCredit($s,$percent);break;
								case '第四学期':$t_4[$u->uId] += $this->getCredit($s,$percent);break;
								case '第五学期':$t_5[$u->uId] += $this->getCredit($s,$percent);break;
								case '第六学期':$t_6[$u->uId] += $this->getCredit($s,$percent);break;
								case '第七学期':$t_7[$u->uId] += $this->getCredit($s,$percent);break;
								case '第八学期':$t_8[$u->uId] += $this->getCredit($s,$percent);break;
							
							}
							
						}
						
					}
					$t_1[$u->uId] = ($u->stuper->term_1*$percent->performance+$t_1[$u->uId])/100.0;
					$t_2[$u->uId] = ($u->stuper->term_2*$percent->performance+$t_2[$u->uId])/100.0;
					$tt_1[$u->uId] = $t_1[$u->uId] + $t_2[$u->uId];
					$t_3[$u->uId] = ($u->stuper->term_3*$percent->performance+$t_3[$u->uId])/100.0;
					$t_4[$u->uId] = ($u->stuper->term_4*$percent->performance+$t_4[$u->uId])/100.0;
					$tt_2[$u->uId] = $t_3[$u->uId] + $t_4[$u->uId] + $tt_1[$u->uId];   //  综合奖学金的排名，修改为 第N学年的排名是前 2N 学期的学习成绩之和的排名
					$t_5[$u->uId] = ($u->stuper->term_5*$percent->performance+$t_5[$u->uId])/100.0;
					$t_6[$u->uId] = ($u->stuper->term_6*$percent->performance+$t_6[$u->uId])/100.0;
					$tt_3[$u->uId] = $t_5[$u->uId] + $t_6[$u->uId] + $tt_2[$u->uId];
					$t_7[$u->uId] = ($u->stuper->term_7*$percent->performance+$t_7[$u->uId])/100.0;
					$t_8[$u->uId] = ($u->stuper->term_8*$percent->performance+$t_8[$u->uId])/100.0;
					$tt_4[$u->uId] = $t_7[$u->uId] + $t_8[$u->uId] + $tt_3[$u->uId];
					
					
				}
				unset($users); //释放内存
				arsort($t_1);arsort($t_2);arsort($tt_1);
				arsort($t_3);arsort($t_4);arsort($tt_2);
				arsort($t_5);arsort($t_6);arsort($tt_3);
				arsort($t_7);arsort($t_8);arsort($tt_4);
				
				$t_1 = $this->setArray($t_1);
				$t_2 = $this->setArray($t_2);
				$tt_1 = $this->setArray($tt_1);
				$t_3 = $this->setArray($t_3);
				$t_4 = $this->setArray($t_4);
				$tt_2 = $this->setArray($tt_2);
				$t_5 = $this->setArray($t_5);
				$t_6 = $this->setArray($t_6);
				$tt_3 = $this->setArray($tt_3);
				$t_7 = $this->setArray($t_7);
				$t_8 = $this->setArray($t_8);
				$tt_4 = $this->setArray($tt_4);
				
				// 从专业中计算
				
				// 大妈代码，我真的不知道该怎么办，真心希望 PHPWord 封装的好一点，有点时间的话，做个 PHPOffice的封装
				$zt_1 = array();
				$zt_2 = array();  $ztt_1 = array();
 				$zt_3 = array();
				$zt_4 = array();  $ztt_2 = array();
				$zt_5 = array();
				$zt_6 = array();  $ztt_3 = array();
				$zt_7 = array();
				$zt_8 = array();  $ztt_4 = array();
				$ci =new CDbCriteria;
				$ci->addCondition("`stuinfo`.major = '".$major."'");
				//$ci->addCondition("`stuinfo`.classRoom = '".$classRoom."'");
				$ci->addCondition("`stuinfo`.grade = '".$grade."'");
				$ci->addCondition("rights = 1");
				$ci->with = array('stuinfo');
				//$ci->addCondition('stuinfo.classRoom = '.$classRoom);
				// 这个专业
				$users = User::model()->findAll($ci);
				$percent = Scholarshippercent::model()->find();
				foreach($users as $u){
					$zt_1[$u->uId] = 0;
					$zt_2[$u->uId] = 0;  $ztt_1[$u->uId] = 0;
					$zt_3[$u->uId] = 0;
					$zt_4[$u->uId] = 0;  $ztt_2[$u->uId] = 0;
					$zt_5[$u->uId] = 0;
					$zt_6[$u->uId] = 0;  $ztt_3[$u->uId] = 0;
					$zt_7[$u->uId] = 0;
					$zt_8[$u->uId] = 0;  $ztt_4[$u->uId] = 0;
					
					if(!$u->stuper)
						$u->stuper = new Stuperformance;
					
					$sches = $u->stusch;
					foreach($sches as $s){
						if($s->condition == 3){
							switch($s->term){
							
								case '第一学期':$zt_1[$u->uId] += $this->getCredit($s,$percent);break;
								case '第二学期':$zt_2[$u->uId] += $this->getCredit($s,$percent);break;
								case '第三学期':$zt_3[$u->uId] += $this->getCredit($s,$percent);break;
								case '第四学期':$zt_4[$u->uId] += $this->getCredit($s,$percent);break;
								case '第五学期':$zt_5[$u->uId] += $this->getCredit($s,$percent);break;
								case '第六学期':$zt_6[$u->uId] += $this->getCredit($s,$percent);break;
								case '第七学期':$zt_7[$u->uId] += $this->getCredit($s,$percent);break;
								case '第八学期':$zt_8[$u->uId] += $this->getCredit($s,$percent);break;
							
							}
							
						}
						
					}
					$zt_1[$u->uId] = ($u->stuper->term_1*$percent->performance+$zt_1[$u->uId])/100.0;
					$zt_2[$u->uId] = ($u->stuper->term_2*$percent->performance+$zt_2[$u->uId])/100.0;
					$ztt_1[$u->uId] = $zt_1[$u->uId] + $zt_2[$u->uId];
					$zt_3[$u->uId] = ($u->stuper->term_3*$percent->performance+$zt_3[$u->uId])/100.0;
					$zt_4[$u->uId] = ($u->stuper->term_4*$percent->performance+$zt_4[$u->uId])/100.0;
					$ztt_2[$u->uId] = $zt_3[$u->uId] + $zt_4[$u->uId] + $ztt_1[$u->uId];  //  综合奖学金的排名，修改为 第N学年的排名是前 2N 学期的学习成绩之和的排名
					$zt_5[$u->uId] = ($u->stuper->term_5*$percent->performance+$zt_5[$u->uId])/100.0;
					$zt_6[$u->uId] = ($u->stuper->term_6*$percent->performance+$zt_6[$u->uId])/100.0;
					$ztt_3[$u->uId] = $zt_5[$u->uId] + $zt_6[$u->uId] + $ztt_2[$u->uId];
					$zt_7[$u->uId] = ($u->stuper->term_7*$percent->performance+$zt_7[$u->uId])/100.0;
					$zt_8[$u->uId] = ($u->stuper->term_8*$percent->performance+$zt_8[$u->uId])/100.0;
					$ztt_4[$u->uId] = $zt_7[$u->uId] + $zt_8[$u->uId] + $ztt_3[$u->uId];
					
					
				}
				unset($users); //释放内存
				arsort($zt_1);arsort($zt_2);arsort($ztt_1);
				arsort($zt_3);arsort($zt_4);arsort($ztt_2);
				arsort($zt_5);arsort($zt_6);arsort($ztt_3);
				arsort($zt_7);arsort($zt_8);arsort($ztt_4);
				
				$zt_1 = $this->setArray($zt_1);
				$zt_2 = $this->setArray($zt_2);
				$ztt_1 = $this->setArray($ztt_1);
				$zt_3 = $this->setArray($zt_3);
				$zt_4 = $this->setArray($zt_4);
				$ztt_2 = $this->setArray($ztt_2);
				$zt_5 = $this->setArray($zt_5);
				$zt_6 = $this->setArray($zt_6);
				$ztt_3 = $this->setArray($ztt_3);
				$zt_7 = $this->setArray($zt_7);
				$zt_8 = $this->setArray($zt_8);
				$ztt_4 = $this->setArray($ztt_4);
				
				Yii::import('application.extensions.phpWord.*');
				require_once 'Autoloader.php';
				\PhpOffice\PhpWord\Autoloader::register();
				$phpWord = new \PhpOffice\PhpWord\PhpWord();
				$styleTable = array('borderSize' => 8, 'borderColor' => '000000', 'cellMargin' => 80);
				$styleFirstRow = array('borderBottomSize' => 18, 'borderBottomColor' => '000000', 'bgColor' => '66BBFF');
				$styleCell = array('valign' => 'center','borderSize' => 8, 'borderColor' => '000000');
				$fontStyle = array('bold' => true, 'align' => 'center');
				$phpWord->addTableStyle('scholarship', $styleTable);
				$phpWord->addFontStyle('header',array('size' => 30, 'bold' => true,'name'=>'Times New Roman'));
				$luokuanStyle = array('bold'=>true,'align'=>'right');
				$section = $phpWord->addSection();
				//print_r($t_1);die();
				foreach($datas as $key => $value){
					
					$headerTextRun = $section->addTextRun(array('align'=>'center'));
					$headerTextRun->addText('教师教育学院 综合奖学金 报表','header');
					
					$section->addTextBreak(1);
					$nameTextRun = $section->addTextRun();
					
					
					$nameTextRun->addText('姓名:'.$value['realName'].'    班级:'.$classRoom.'   年级:'.$grade.'     专业:'.$major.'     学号：'.$value['username']);
					$section->addTextBreak(1);
					$section->addTextBreak(1);
					$table = $section->addTable('scholarship');
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText('综合成绩', $fontStyle);
					$table->addCell(2250, $styleCell)->addText('班级排名', $fontStyle);
					$table->addCell(2250, $styleCell)->addText('专业排名', $fontStyle);
					
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第一学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_1[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_1[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_1[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第二学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_2[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_2[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_2[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第一学年', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_1[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_1[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($ztt_1[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第三学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_3[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_3[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_3[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第四学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_4[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_4[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_4[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第二学年', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_2[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_2[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($ztt_2[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第五学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_5[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_5[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_5[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第六学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_6[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_6[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_6[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第三学年', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_3[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_3[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($ztt_3[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第七学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_7[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_7[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_7[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第八学期', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_8[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($t_8[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($zt_8[$value['uId']]['noun'], $fontStyle);
					
					$table->addRow(600);
					$table->addCell(2250, $styleCell)->addText('第四学年', $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_4[$value['uId']]['per'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($tt_4[$value['uId']]['noun'], $fontStyle);
					$table->addCell(2250, $styleCell)->addText($ztt_4[$value['uId']]['noun'], $fontStyle);
					
					// 落款
					$section->addTextBreak(1);
					$section->addTextBreak(1);
					$section->addTextBreak(1);
					$section->addTextBreak(1);
					$section->addTextBreak(1);
					$section->addTextBreak(1);
					$section->addTextBreak(1);
					$luokuanTextRun1 = $section->addTextRun(array('align'=>'right','bold'=>true));
					$luokuanTextRun1->addText('重庆第二师范学院教师教育学院');
					$section->addTextBreak(1);
					$luokuanTextRun2 = $section->addTextRun(array('align'=>'right','bold'=>true));
					$luokuanTextRun2->addText(date('Y-m-d'));
					$section->addPageBreak();
					
				}
				$this->downloadWord(date('Y-m-d').'综合奖学金导出-'.count($datas).'人',$phpWord);
			
		}else{
			
			$this->logError('invalid access');
			echo 'logged !!!!';
		}
		
		
	}
	private function setArray($a){
		
		$i = 0;
		foreach($a as $key => $value){
			$i++;
			$a[$key] = array('per'=>$value,'noun'=>$i);
		}
		return $a;
		
		
	}
	private function getCredit($s,$percent){
		$re = 0;
		
		switch($s->module){
			case '品德行为':$re = $s->credit*$percent->morality;break;
			case '文体综合素质':$re = $s->credit*$percent->physical;break;
			case '学业表现':$re = $s->credit*$percent->integrate;break;	
		}
		return $re;
	}		 
}
?>